package com.example.sale.Controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.example.sale.Mapper.UserDetailMapper;
import com.example.sale.Service.IProductService;
import com.example.sale.Service.IUserDetailService;
import com.example.sale.True.Product;
import com.example.sale.True.UserDetail;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("excel")
public class ExcelController {
    @Autowired
    private  IUserDetailService iUserDetailService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    UserDetailMapper userDetailMapper;
    @RequestMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //查询数据库当中所有的数据
        List<UserDetail> Users= iUserDetailService.list();
        //生成excel
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("详情数据1", "数据信息"), UserDetail.class, Users);
        response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("用户详情信息.xls","UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
    @RequestMapping("/exportProduct")
    public void exportProductExcel(HttpServletResponse response) throws IOException {
        //查询数据库当中所有的数据
        List<Product> products= iProductService.list();
        System.out.println(products);
        //生成excel
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("详情数据1", "数据信息"), Product.class, products);
        response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("商品详情信息.xls","UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
}
